package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.ExerciseRank;
import com.example.demo.mapper.ExerciseRankMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/***
 ** Create by Wedream on 2021/7/16
 */
@RestController
@RequestMapping("/exerciseRank")
public class ExerciseRankController {
    
    @Resource
    ExerciseRankMapper exerciserankMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody ExerciseRank exerciserank) {
        exerciserankMapper.insert(exerciserank);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        exerciserankMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<ExerciseRank> wrapper = Wrappers.<ExerciseRank>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(ExerciseRank::getCompany,search);
        }

        Page<ExerciseRank> exerciserankPage = exerciserankMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(exerciserankPage);
//        return Result.success();
    }

    @PutMapping //改
    public Result<?> update(@RequestBody ExerciseRank exerciserank) {
        exerciserankMapper.updateById(exerciserank);
        return Result.success();
    }
}
